import { useWebConfigStore,useUserStore,useRefs } from "@/stores";
import {$http} from './http';
import {$message} from './message';
import system from './mp-weixin/system';

const buildOptions=(options:any)=>{
    return options;
}
export const $page={
	open:(params:UniApp.NavigateToOptions | string)=>{
		let options:UniApp.NavigateToOptions={
			url:''
		};
		if(typeof params==='string'){
			options.url=params;
		}else{
			options=Object.assign(options,params);
		}
		uni.navigateTo(buildOptions(options));
	},
	redirect:(params:UniApp.RedirectToOptions | string)=>{
		let options:UniApp.RedirectToOptions={
			url:''
		};
		if(typeof params==='string'){
			options.url=params;
		}else{
			options=Object.assign(options,params);
		}
		uni.redirectTo(buildOptions(options))
	},
	re:(params:UniApp.ReLaunchOptions | string)=>{
		let options:UniApp.ReLaunchOptions={
			url:''
		};
		if(typeof params==='string'){
			options.url=params;
		}else{
			options=Object.assign(options,params);
		}
		uni.reLaunch(buildOptions(options))
	},
	switch:(params:UniApp.SwitchTabOptions|string)=>{
		let options:UniApp.SwitchTabOptions={
			url:''
		};
		if(typeof params==='string'){
			options.url=params;
		}else{
			options=Object.assign(options,params);
		}
		uni.switchTab(buildOptions(options));
	},
	title:(title?:string,subtitle?:string)=>{
		const webConfigStore=useWebConfigStore();
		const {WEBCONFIG}=useRefs(webConfigStore);
		const fastTitle=title||WEBCONFIG.value?.web_name||system.name();
		const lastTitle=subtitle!==undefined?subtitle:'';
		uni.setNavigationBarTitle({
			title:fastTitle+lastTitle
		})
	},
	shareMessage(options:any){
		const webConfigStore=useWebConfigStore();
		const {WEBCONFIG}=useRefs(webConfigStore);
		const userStore=useUserStore();
		let obj:any={
			...options
		};
		obj.title=WEBCONFIG.value?.share_title||options.title||system.name();
		obj.imageUrl=WEBCONFIG.value?.share_image||options.imageUrl||'';
		if(userStore.hasLogin()){
			const {USERINFO}=useRefs(userStore);
			if(obj.path.indexOf('?')>-1){
				obj.path+='&';
			}else{
				obj.path+='?';
			}
			obj.path+=`puid=${USERINFO.value.id}`;
		}
		return obj;
	},
	shareTimeline(options:any){
		const webConfigStore=useWebConfigStore();
		const {WEBCONFIG}=useRefs(webConfigStore);
		const userStore=useUserStore();
		let obj:any={
			...options
		};
		obj.title=WEBCONFIG.value?.share_title||options.title||system.name();
		obj.imageUrl=WEBCONFIG.value?.share_image||options.imageUrl||'';
		if(userStore.hasLogin()){
			const {USERINFO}=useRefs(userStore);
			if(obj.path.indexOf('?')>-1){
				obj.path+='&';
			}else{
				obj.path+='?';
			}
			obj.path+=`puid=${USERINFO.value.id}`;
		}
		return obj;
	},
}
export const login=(data:any,xLoginRef:any)=>{
	const webConfigStore=useWebConfigStore();
	const {WEBCONFIG}=useRefs(webConfigStore);
	if(system.isWechat()&&WEBCONFIG.value?.wx_state){
		// #ifdef H5
		$http.post('Login/wechatLogin', {
			data
		})
			.then((res: any) => {
				if (res.code === $http.ResponseCode.SUCCESS) {
					globalThis.location.href = res.data;
				} else {
					$message.error(res.msg);
				}
			}).catch((err: any) => {
				$message.error(err.msg);
			})
		// #endif
	}else{
		xLoginRef.value.show();
	}
}